<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left">
        <h1>实验室设备管理系统</h1>
        <p>高效、便捷的设备管理解决方案</p>
      </div>
      <div class="login-right">
        <a-card class="login-card">
          <template #title>
            <h2>用户登录</h2>
          </template>
          <a-form
            :model="formState"
            name="login"
            @finish="onFinish"
            autocomplete="off"
          >
            <a-form-item
              name="user_id"
              :rules="[
                { required: true, message: '请输入用户ID' },
                { min: 6, message: '用户ID至少6位' },
                { pattern: /^\d+$/, message: '用户ID只能包含数字' }
              ]"
            >
              <a-input v-model:value="formState.user_id" placeholder="用户ID（至少6位数字）">
                <template #prefix>
                  <user-outlined />
                </template>
              </a-input>
            </a-form-item>

            <a-form-item
              name="password_hash"
              :rules="[
                { required: true, message: '请输入密码' },
                { min: 6, message: '密码至少6位' },
                { 
                  pattern: /^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{6,}$/,
                  message: '密码必须包含字母、数字和特殊符号'
                }
              ]"
            >
              <a-input-password v-model:value="formState.password_hash" placeholder="密码（字母+数字+特殊符号）">
                <template #prefix>
                  <lock-outlined />
                </template>
              </a-input-password>
            </a-form-item>

            <a-form-item>
              <a-button type="primary" html-type="submit" block>
                登录
              </a-button>
            </a-form-item>

            <div class="login-options">
              <a-button type="link" @click="goToRegister">
                注册账号
              </a-button>
            </div>
          </a-form>
        </a-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'

const router = useRouter()

const formState = reactive({
  user_id: '',
  password_hash: ''
})

const onFinish = async (values) => {
  try {
    const response = await fetch('http://localhost:8080/api/auth/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(values)
    })

    if (response.ok) {
      const data = await response.json()
      localStorage.setItem('token', data.token)
      message.success('登录成功')
      router.push('/')
    } else {
      message.error('登录失败：用户名或密码错误')
    }
  } catch (error) {
    message.error('登录失败：' + error.message)
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1890ff 0%, #722ed1 100%);
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: 0;
  padding: 0;
}

.login-content {
  width: 100%;
  max-width: 1200px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  margin: 0;
}

.login-left {
  flex: 1;
  color: white;
  padding: 40px;
  text-align: center;
  min-width: 0; /* 防止内容溢出 */
}

.login-left h1 {
  font-size: 2.5em;
  margin-bottom: 20px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.login-left p {
  font-size: 1.2em;
  opacity: 0.9;
}

.login-right {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  min-width: 0; /* 防止内容溢出 */
}

.login-card {
  width: 100%;
  max-width: 400px;
  min-width: 280px; /* 设置最小宽度 */
  background: rgba(255, 255, 255, 0.95);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.login-card :deep(.ant-card-head-title) {
  text-align: center;
  font-size: 24px;
  color: #1890ff;
}

.login-options {
  display: flex;
  justify-content: center;
  margin-top: 16px;
}

/* 响应式布局 */
@media screen and (max-width: 768px) {
  .login-content {
    flex-direction: column;
    padding: 10px;
    height: auto;
    min-height: 100%;
  }

  .login-left {
    padding: 20px;
    width: 100%;
  }

  .login-right {
    padding: 10px;
    width: 100%;
  }

  .login-card {
    max-width: 100%;
    min-width: 280px;
  }
}

/* 确保在最小尺寸下也能正常显示 */
@media screen and (max-width: 320px) {
  .login-content {
    padding: 5px;
  }

  .login-left {
    padding: 10px;
  }

  .login-right {
    padding: 5px;
  }

  .login-card {
    min-width: 260px;
  }
}
</style> 